/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.beans;

import entities.PublicRelations;
import entities.User;
import entities.UserType;
import facades.PublicRelationsFacade;
import facades.UserFacade;
import facades.UserTypeFacade;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;
import qualifiers.CreatedEntityEvent;
import qualifiers.PreCreateEntityEvent;
import util.Sha256Encrypter;

/**
 *
 * @author Alvaro
 */
@Named(value = "adminNewPublicRelationsBean")
@SessionScoped
public class AdminNewPublicRelationsBean implements Serializable {

    @Inject
    @PreCreateEntityEvent
    private Event<PublicRelations> preCreatePublicRelationshipsEvent;
    @Inject
    @CreatedEntityEvent
    private Event<PublicRelations> createPublicRelationshipsEvent;
    @Inject
    @PreCreateEntityEvent
    private Event<User> preCreateUserEvent;
    @Inject
    @CreatedEntityEvent
    private Event<User> createUserEvent;
    @Inject
    private PublicRelationsFacade publicRelationsFacade;
    @Inject
    private UserFacade userFacade;
    @Inject
    private UserTypeFacade userTypeFacade;
    private PublicRelations publicRelations;
    /**
     * Creates a new instance of AdminNewPublicRelationshipsBean
     */
    public AdminNewPublicRelationsBean() {
        publicRelations = new PublicRelations();
        publicRelations.setUser(new User());

    }

    /**
     * @return the publicRelations
     */
    public PublicRelations getPublicRelationship() {
        return publicRelations;
    }

    public void createPublicRelationships() {
        publicRelations.setReferals(0);
        publicRelations.getUser().setActive(true);
        publicRelations.getUser().setLastDate(new Date());
        publicRelations.getUser().setCreationDate(new Date());
        publicRelations.getUser().setLogins(0);
        UserType userType = userTypeFacade.find(4);
        publicRelations.getUser().setUserType(userType);
        try {
            publicRelations.getUser().setPassword(Sha256Encrypter.getSha256(publicRelations.getUser().getPassword()));
        } catch (NoSuchAlgorithmException ex) {
//            Logger.getLogger(CreateCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
//            Logger.getLogger(CreateCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        publicRelations.getUser().setPublicRelations(publicRelations);
        userFacade.create(publicRelations.getUser());
        createUserEvent.fire(publicRelations.getUser());
        createPublicRelationshipsEvent.fire(publicRelations.getUser().getPublicRelations());

    }

    /**
     * @param publicRelations the publicRelations to set
     */
    public void setPublicRelationship(PublicRelations publicRelations) {
        this.publicRelations = publicRelations;
    }
}
